﻿//分页
!function ($) {
    //定义对象
    $.zgrid = $.zgrid || {};
    //扩展接口
    $.extend($.zgrid, {
        pagination: function (page, size, count) {
            var totalPage = parseInt((parseInt(count) + parseInt(size) - 1) / parseInt(size));
            //显示可点击页码总数
            var showPageCount = 10;
            //当前页面是否是showPageCount整数倍
            var curPageIsShowPageCountTimes = parseInt(parseInt(page) % showPageCount);
            var leftPage = parseInt(parseInt(page) / showPageCount);
            if (curPageIsShowPageCountTimes === 0) {
                leftPage = leftPage - 1;
            }
            leftPage = leftPage * showPageCount + 1;
            //剩余页数
            var leftCount = parseInt(totalPage % showPageCount);
            if (leftCount === 0) {
                leftCount = showPageCount;
            }
            var rightPage = leftPage + leftCount - 1;
            var pagination = '<div class="col-xs-3"><div class="dataTables_info">第' + page + '页,共' + totalPage + '页,共' + count + '条</div></div>'
                + '<div class="col-xs-7">'
                + '<div class="dataTables_paginate paging_simple_numbers">'
                + '<ul class="pagination">'
                + '<li class="paginate_button previous"><a href="javascript:;"><<</a></li>';
            for (var i = leftPage; i <= rightPage; i++) {
                if (i === page) {
                    pagination += '<li class="paginate_button active"><a href="javascript:;">' + i + '</a></li>';
                } else {
                    pagination += '<li class="paginate_button "><a href="javascript:;">' + i + '</a></li>';
                }
            }
            pagination += '<li class="paginate_button next"><a href="javascript:;">>></a></li>';
            pagination += '</ul>';
            pagination += '</div>';
            pagination += '</div>';
            pagination += '<div class="col-xs-2"><div class="dataTables_info">第&nbsp;<input type="text" style="width: 30px;" />&nbsp;页<span>&nbsp;&nbsp;GO</span></div></div>';
            $("#grid-pager").html(pagination);
            $.zgrid.paginationClass(page, totalPage);
        },
        paginationClass: function (page, totalPage) {
            if (page === 1) {
                $('ul.pagination li.previous').addClass("disabled");
            }
            if (page === totalPage) {
                $('ul.pagination li.next').addClass("disabled");
            }
        }
    });
    //点击页面事件
    $(document).on("click", ".pagination li", function () {
        var $this = $(this);
        if ($this.hasClass("disabled") || $this.hasClass("active")) {
            return;
        }
        var index = parseInt($(".pagination li.active").find("a").text());
        if ($this.hasClass("previous")) {
            if (index === 1) {
                return;
            }
            $.zgrid.Request.callBackGetPager(index - 1);
            return;
        }
        if ($this.hasClass("next")) {
            $.zgrid.Request.callBackGetPager(index + 1);
            return;
        }
        $.zgrid.Request.callBackGetPager(parseInt($this.find("a").text()));
    });
    $(document).on("mouseover", "table.hovertable tbody tr", function () {
        $(this).addClass("trmouserover");
    });
    $(document).on("mouseout", "table.hovertable tbody tr", function () {
        $(this).removeClass("trmouserover");
    });

}(jQuery);
//列表
!function ($) {
    //定义对象
    $.zgrid = $.zgrid || {};
    //扩展接口
    $.extend($.zgrid, {
        Version: '1.0.0',
        Request: {
            url: '',
            type: 'POST',
            callBackGetPager: function (page) { },
            data: {},
            success: function () { }
        },
        AjaxData: function () {
            $.ajax({
                url: $.zgrid.Request.url,
                type: $.zgrid.Request.type,
                data: $.zgrid.Request.data,
                success: $.zgrid.Request.success
            });
        }
    });
    //jquery 实例扩展
    $.fn.zGrid = function (pin) {
        $.zgrid.Request = pin;
        $.zgrid.AjaxData();
    };
}(jQuery);
